package com.qilu.pe.ui.activity;

import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatEditText;

import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.Logg;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.util.HandlerUtil;
import com.ruitu.arad.util.ToastUtils;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// 修改密码
public class ChangePwdActivity extends Base2Activity implements HandlerUtil.OnReceiveMessageListener {
    private AppCompatEditText et_phone, et_code, et_pwd, et_pwd2;// 手机号，验证码，密码,确认密码
    private TextView tv_send_code, tv_ok;// 发送验证码,确认

    String phone;
    String code;
    String pwd;
    String pwd2;

    private boolean isCd = false;// 是否正在倒计时
    private int currSecond = 60;// 当前倒计时的秒数

    private HandlerUtil.HandlerHolder handler = new HandlerUtil.HandlerHolder(this);

    @Override
    public void handlerMessage(Message msg) {
        if (msg.what == 20101951) {
            currSecond--;
            if (currSecond <= 0) {
                tv_send_code.setText("发送验证码");
                currSecond = 60;
                isCd = false;// 取消倒计时状态
            } else {
                tv_send_code.setText(currSecond + "s");
                handler.sendEmptyMessageDelayed(20101951, 1000);
            }
        }
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_change_pwd;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        et_phone = findViewById(R.id.et_phone);
        et_code = findViewById(R.id.et_code);
        et_pwd = findViewById(R.id.et_pwd);
        et_pwd2 = findViewById(R.id.et_pwd2);
        tv_send_code = findViewById(R.id.tv_send_code);
        tv_ok = findViewById(R.id.tv_ok);

        setHeadTitle("修改密码");

        setOnClickListener(tv_send_code, tv_ok);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_send_code) {
            if (!isCd) {
                phone = et_phone.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtils.showShort("请输入手机号");
                    return;
                }
                getMsgCode();
                showProgress();
                isCd = true;
                handler.sendEmptyMessage(20101951);
            }
        }
        if (tv_ok == tv_ok) {
            phone = et_phone.getText().toString().trim();
            code = et_code.getText().toString().trim();
            pwd = et_pwd.getText().toString().trim();
            pwd2 = et_pwd2.getText().toString().trim();
            if (TextUtils.isEmpty(phone)) {
                ToastUtils.showShort("请输入手机号");
                return;
            }
            if (TextUtils.isEmpty(code)) {
                ToastUtils.showShort("请输入验证码");
                return;
            }
            if (TextUtils.isEmpty(pwd)) {
                ToastUtils.showShort("请输入密码");
                return;
            }
            if (TextUtils.isEmpty(pwd2)) {
                ToastUtils.showShort("请确认密码");
                return;
            }
            if (!TextUtils.equals(pwd, pwd2)) {
                ToastUtils.showShort("两次输入的密码不一致!");
                return;
            }

            hideProgress();
            changePwd();
            showProgress();
        }
    }

    public Disposable getMsgCode() {
        return APIRetrofit.getDefault().getMsgCode(phone)
                .compose(RxUtil.<BaseResult2>subIoObMain())
                .subscribe(new Consumer<BaseResult2>() {
                    @Override
                    public void accept(BaseResult2 baseResult2) throws Exception {
                        hideProgress();
                        Logg.i("baseResult2 = " + baseResult2);
                        if (baseResult2.isSuccess()) {
                            ToastUtils.showShort(baseResult2.getMsg());
                        } else {
                            ToastUtils.showShort(baseResult2.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        ToastUtils.showShort("发送失败，请重试");
                    }
                });
    }

    public Disposable changePwd() {
        return APIRetrofit.getDefault().reqChangePwd(Global.HEADER, phone, code, pwd)
                .compose(RxUtil.<BaseResult2>subIoObMain())
                .subscribe(new Consumer<BaseResult2>() {
                    @Override
                    public void accept(BaseResult2 baseResult2) throws Exception {
                        hideProgress();
                        if (baseResult2.isSuccess()) {
                            ToastUtils.showShort(baseResult2.getMsg());
                            finish();
                        } else {
                            ToastUtils.showShort(baseResult2.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        ToastUtils.showShort("密码修改失败，请重试");
                    }
                });
    }
}